package com.cgwx.systemservice.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cgwx.springsecurity.annotation.Log;
import com.cgwx.springsecurity.enums.BusinessType;
import com.cgwx.commonutils.result.SysResult;
import com.cgwx.systemservice.entity.sys.SysRole;
import com.cgwx.systemservice.entity.vo.SysRoleQueryVo;
import com.cgwx.systemservice.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 角色 前端控制器
 * </p>
 *
 * @author GaoWei
 * @since 2023-10-12
 */

@Api(tags = "角色前端控制器")
@RestController
@RequestMapping("/system/sysRole")
@CrossOrigin
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation("查询所有角色")
    @GetMapping("findAll")
    public SysResult findAllRole() {
        List<SysRole> list = sysRoleService.list();
        return SysResult.ok(list);
    }

    @Log(title = "删除角色", businessType = BusinessType.DELETE)
    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation("根据id逻辑删除角色")
    @DeleteMapping("remove/{id}")
    public SysResult removeRole(
            @ApiParam(name = "id", value = "待删除的角色id")
            @PathVariable Long id) {
        boolean isSuccess = sysRoleService.removeById(id);
        if(isSuccess) {
            return SysResult.ok();
        } else {
            return SysResult.fail();
        }
    }

    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation("条件分页查询角色")
    @GetMapping("{page}/{limit}")
    public SysResult findPageQueryRole(
            @ApiParam(name = "page", value = "当前页")
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页显示的记录数")
            @PathVariable Long limit,
            @ApiParam(name = "sysRoleQueryVo", value = "待查询的角色条件Vo")
            SysRoleQueryVo sysRoleQueryVo) {
        //创建page对象
        Page<SysRole> pageParam = new Page<>(page,limit);
        //调用service方法
        IPage<SysRole> pageModel = sysRoleService.selectPage(pageParam,sysRoleQueryVo);
        return SysResult.ok(pageModel);
    }

    @ApiOperation(value = "根据ID获取角色")
    @GetMapping("getById/{id}")
    public SysResult getById(
            @ApiParam(name = "id", value = "待获取的角色id")
            @PathVariable Long id) {
        SysRole role = sysRoleService.getById(id);
        return SysResult.ok(role);
    }

    @Log(title = "新增角色", businessType = BusinessType.INSERT)
    @PreAuthorize("hasAuthority('bnt.sysRole.add')")
    @ApiOperation(value = "新增角色")
    @PostMapping("save")
    public SysResult save(
            @ApiParam(name = "role", value = "待新增的角色实体类")
            @RequestBody SysRole role) {
        sysRoleService.save(role);
        return SysResult.ok();
    }

    @Log(title = "修改角色", businessType = BusinessType.UPDATE)
    @PreAuthorize("hasAuthority('bnt.sysRole.update')")
    @ApiOperation(value = "修改角色")
    @PostMapping("update")
    public SysResult update(
            @ApiParam(name = "role", value = "待修改的角色实体类")
            @RequestBody SysRole role) {
        sysRoleService.updateById(role);
        return SysResult.ok();
    }

    @Log(title = "批量删除角色", businessType = BusinessType.DELETE)
    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation(value = "根据id列表批量删除")
    @DeleteMapping("batchRemove")
    public SysResult batchRemove(
            @ApiParam(name = "idList", value = "待修改的角色ID列表")
            @RequestBody List<Long> idList) {
        sysRoleService.removeByIds(idList);
        return SysResult.ok();
    }
}

